using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;
using log4net.Repository.Hierarchy;
using IGlobalist.Cms.Common;
using System.Text;
using System.Collections.Generic;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class HomeAddEdit : System.Web.UI.UserControl
    {
        public string xmlHomeCat = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/Admin/XML/HomeCategories.xml");
        public string xmlLMSCat = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/Admin/XML/Categories.xml");
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void DoHidden(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        public void DoShow(object sender, EventArgs e)
        {
            this.Visible = true;
        }
        

        public void DoEditCategoryEdu(object sender, EventArgs e)
        {
            HomeTree ucPageTree = (HomeTree)sender;
            long pageID = Convert.ToInt64(ucPageTree.SelectedValue);
            BindPage(pageID);
        }

        private void BindPage(long pageID)
        {
            ViewState["SelectedHomeCatID"] = pageID;             
            // CategoryEdu<CategoryEdu> catEduList  = new CategoryEdu<CategoryEdu>(xmlHomeCat);
             //CategoryEdu catEdu = catEduList.GetItemRelationById(pageID);

            CategoryXML catEdu = CategoryXMLCtr.GetCategoryByID(pageID);             
             if (catEdu != null)
             {                 
                 IList<long> catArray = new List<long>();
                 IList<long> catLMSArray = new List<long>();
                  
                 foreach (long catID in catEdu.Subs)
                 {                     
                     if (catID < 1000000)
                     {
                         catArray.Add(catID);
                     }
                     else
                     {
                         catLMSArray.Add(catID);
                     }                     
                 }
                 if (catEdu != null)
                 {
                     BindCategoryPageType(catLMSArray);
                     BindCategory(catArray);
                 }

             }
             else
             {
                 BindCategoryPageType();
                 BindCategories();
             }
               

        }

        private void BindCategory(IList<long> catLMSArray)
        {
            tvCategory.Nodes.Clear();
            IContentCategory root = CategoryController.GetCategoryTree();
            TreeNode nodeRoot = new TreeNode("Categories", "0");
            nodeRoot.NavigateUrl = "javascript:void(0)";
            PopulateBindCatPageType(root, nodeRoot, catLMSArray);
            tvCategory.Nodes.Add(nodeRoot);
            tvCategory.ExpandAll();
            tvCategory.Nodes[0].ShowCheckBox = false;
            tvCategory.Nodes[0].Checked = false;
        }

        private void BindCategoryPageType(IList<long> catEdu)
        {
            tvLMSCategory.Nodes.Clear();
            
            IContentCategory root = CategoryController.GetCategoryXMLList(false);
            TreeNode nodeRoot = new TreeNode("LMS Categories", "0");
            nodeRoot.NavigateUrl = "javascript:void(0)";
            PopulateBindCatPageType(root, nodeRoot, catEdu);
            tvLMSCategory.Nodes.Add(nodeRoot);

            tvLMSCategory.ExpandAll();
            tvLMSCategory.Nodes[0].ShowCheckBox = false;
            tvLMSCategory.Nodes[0].Checked = false;
        }
        public void BindCategories()
        {
            tvCategory.Nodes.Clear();
            IContentCategory root = CategoryController.GetCategoryTree();
            TreeNode nodeRoot = new TreeNode("Categories", "0");
            nodeRoot.NavigateUrl = "javascript:void(0)";
            PopulateBind(root, nodeRoot);
            tvCategory.Nodes.Add(nodeRoot);
            tvCategory.ExpandAll();
            tvCategory.Nodes[0].ShowCheckBox = false;
            tvCategory.Nodes[0].Checked = false;
        }

        private void PopulateBind(IContentCategory category, TreeNode node)
        {
            ContentCategoryList list = new ContentCategoryList();
            if (ViewState["CategoryList"] != null)
                list = (ContentCategoryList)ViewState["CategoryList"];

            if (category.CategoryList != null)
                foreach (IContentCategory cat in category.CategoryList)
                {
                    TreeNode subNode = new TreeNode(cat.Name, cat.ID.ToString());
                    foreach (IContentCategory c in list)
                    {
                        if (c.ID == Convert.ToInt64(subNode.Value))
                            subNode.Checked = true;
                    }
                    node.ChildNodes.Add(subNode);
                    PopulateBind(cat, subNode);
                }
        }


        private void PopulateBindCatPageType(IContentCategory category, TreeNode node, IList<long> iRelation)
        {
            ContentCategoryList list = new ContentCategoryList();
            if (iRelation != null)
            {                
                foreach (IContentCategory cat in category.CategoryList)
                {
                    TreeNode subNode = new TreeNode(cat.Name, cat.ID.ToString());
                    for (int i = 0; i < iRelation.Count; i++)
                    {
                        if (subNode.Value == iRelation[i].ToString())
                            subNode.Checked = true;
                    }
                    node.ChildNodes.Add(subNode);
                    PopulateBindCatPageType(cat, subNode, iRelation);
                }
            }
        }     
                


        public void DoSaveCategory(object sender, EventArgs e)
        {            
            IList<long> subList = new List<long>();
            foreach (TreeNode trenode in tvLMSCategory.Nodes)
            {
                foreach (TreeNode childroot in trenode.ChildNodes)
                {
                    if (childroot.Checked == true)
                    {                        
                        subList.Add(Convert.ToInt64(childroot.Value));
                    }
                    else
                    {
                        foreach (TreeNode cc in childroot.ChildNodes)
                        {
                            if (cc.Checked == true)
                            {
                                subList.Add(Convert.ToInt64(cc.Value));
                            }
                        }
                    }
                }
            }

            foreach (TreeNode trenode in tvCategory.Nodes)
            {
                foreach (TreeNode childroot in trenode.ChildNodes)
                {
                    if (childroot.Checked == true)
                    {
                        subList.Add(Convert.ToInt64(childroot.Value));
                    }
                    else
                    {
                        foreach (TreeNode cc in childroot.ChildNodes)
                        {
                            if (cc.Checked == true)
                            {
                                subList.Add(Convert.ToInt64(cc.Value));
                            }
                        }
                    }
                }
            }
            

            long pageID = 0;
            if (ViewState["SelectedHomeCatID"] != null)
            {
                pageID = Convert.ToInt64(ViewState["SelectedHomeCatID"]);
            }

            CategoryXMLCtr.AddSub(pageID, subList);
        }


        private void PopulateBindCatPageType(IContentCategory category, TreeNode node)
        {
            ContentCategoryList list = new ContentCategoryList();
            if (ViewState["CategoryList"] != null)
                list = (ContentCategoryList)ViewState["CategoryList"];

            if (category.CategoryList != null)
                foreach (IContentCategory cat in category.CategoryList)
                {
                    TreeNode subNode = new TreeNode(cat.Name, cat.ID.ToString());
                    foreach (IContentCategory c in list)
                    {
                        if (c.ID == Convert.ToInt64(subNode.Value))
                            subNode.Checked = true;
                    }
                    node.ChildNodes.Add(subNode);
                    PopulateBindCatPageType(cat, subNode);
                }
        }

        public void BindCategoryPageType()
        {
            tvLMSCategory.Nodes.Clear();            
            IContentCategory root = CategoryController.GetCategoryXMLList(true);
            TreeNode nodeRoot = new TreeNode("LMS Categories", "0");
            nodeRoot.NavigateUrl = "javascript:void(0)";
            PopulateBindCatPageType(root, nodeRoot);
            tvLMSCategory.Nodes.Add(nodeRoot);
            tvLMSCategory.ExpandAll();
            tvLMSCategory.Nodes[0].ShowCheckBox = false;
            tvLMSCategory.Nodes[0].Checked = false;
        }
    }
}